/***************************************************************************
 *   Copyright (C) 2008 by Rysiek Król,,,   *
 *   rysiek@Babylon2   *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
#include "imageloader.h"
#include "picturemanager.h"
#include "picturelistwidgetitem.h"
#include "imageloadedevent.h"
#include <QApplication>
#include <QLabel>
#include <QDesktopWidget>

/**
* class Constructor
*/
ImageLoader::ImageLoader( PictureManager *manager)
 : QThread()
{
	this->manager = manager;
	this->start ( QThread::LowPriority );
	this->goToSleep = true;
	this->size = QApplication::desktop()->size();
	this->connect( this->manager->getItsProperty() , SIGNAL ( propertyChanged(Property::PropertyType) ), this, SLOT ( propertyChangedSlot(Property::PropertyType) ) );
}

/**
* Class Destructor
*/
ImageLoader::~ImageLoader()
{
}

/**
* Main thread method
*/
void ImageLoader::run()
{
	while(true){
		this->waitMutex.lock();
		if (this->goToSleep) this->newActionCondition.wait(&this->waitMutex);
		this->waitMutex.unlock();
		this->goToSleep = true;
		switch(this->currentAction){
			case(ImageLoader::GetIndex):
				this->sendIndexedImage();
			break;
			case(ImageLoader::GetNext):
				this->sendNextImage();
			break;
			case(ImageLoader::GetPrevious):
				this->sendPreviousImage();
			break;
			case ImageLoader::GetCurrent:
				this->sendCurrentImage();
			break;
			case ImageLoader::GetNextAndReload:
				this->sendNextAndReload();
		}
	}
}

/**
* Returning current image Index
*/
int ImageLoader::getCurrentIndex() const
{
        return currentIndex;
}

/**
* Setting current Index
*/
void ImageLoader::setCurrentIndex(int theValue)
{
        currentIndex = theValue;
}

/**
* Sending Indexed Image to the Thread and load other images
*/
void ImageLoader::sendIndexedImage()
{
	
	PictureListWidgetItem *tmpItem;
	tmpItem = dynamic_cast<PictureListWidgetItem*> ( this->manager->getListWidget()->item(this->currentIndex));
        this->currentImage.load(tmpItem->getFile().filePath());
        this->currentImage = getScaledImage(this->currentImage);
        QApplication::postEvent ( this->manager, new ImageLoadedEvent ( this->currentImage, this->currentIndex ));


        if ((this->currentIndex+1)<this->manager->getListWidget()->count()) {
                tmpItem = dynamic_cast<PictureListWidgetItem*> ( this->manager->getListWidget()->item(this->currentIndex+1));
                this->nextImage.load(tmpItem->getFile().filePath());
		this->nextImage = getScaledImage(this->nextImage);
        }
        if ((this->currentIndex-1)<this->manager->getListWidget()->count()) {
                tmpItem = dynamic_cast<PictureListWidgetItem*> ( this->manager->getListWidget()->item(this->currentIndex-1));
                if (!tmpItem->getFile().isDir()) {
                        this->previousImage.load(tmpItem->getFile().filePath());
			this->previousImage=getScaledImage(this->previousImage);
                }
        }
}

/**
* returning current Action
*/
ImageLoader::Action ImageLoader::getCurrentAction() const
{
        return currentAction;
}

/**
* Setting  current action variable
*/
void ImageLoader::setCurrentAction(const Action& theValue)
{
        this->currentAction = theValue;
}

/**
* Sending next image to the GUI Thread
*/
void ImageLoader::sendNextImage()
{
	PictureListWidgetItem *tmpItem;
	QApplication::postEvent ( this->manager, new ImageLoadedEvent ( this->nextImage, ++this->currentIndex));
	this->previousImage = this->currentImage;
	this->currentImage = this->nextImage;
        if ((this->currentIndex+1)<this->manager->getListWidget()->count()) {
                tmpItem = dynamic_cast<PictureListWidgetItem*> ( this->manager->getListWidget()->item(this->currentIndex+1));	
                this->nextImage.load(tmpItem->getFile().filePath());
		this->nextImage=getScaledImage(this->nextImage);
        }
}

/**
* Sending previous image to the GUI Thread
*/
void ImageLoader::sendPreviousImage()
{
PictureListWidgetItem *tmpItem;

	QApplication::postEvent ( this->manager, new ImageLoadedEvent ( this->previousImage, --this->currentIndex));
	this->nextImage = this->currentImage;
	this->currentImage = this->previousImage;
        if ((this->currentIndex-1)<this->manager->getListWidget()->count()) {
                tmpItem = dynamic_cast<PictureListWidgetItem*> ( this->manager->getListWidget()->item(this->currentIndex-1));
                if (!tmpItem->getFile().isDir()) {
                        this->previousImage.load(tmpItem->getFile().filePath());
                        this->previousImage=getScaledImage(this->previousImage);
                }
        }
}

/**
* Setting goToSleep Flag to False
*/
void ImageLoader::setGoToSleepFalse()
{
	this->goToSleep = false;
}

/**
* Sending current image to the GUI Thread
*/
void ImageLoader::sendCurrentImage()
{
QApplication::postEvent ( this->manager, new ImageLoadedEvent ( this->currentImage, this->currentIndex));
}

/**
* Zwraca odpowiednio przeskalowany obraz
*/
QImage ImageLoader::getScaledImage(QImage image)
{
        if (!this->manager->getItsProperty()->getShowScalledPictures()) {
                if (this->size.width()>image.size().width() && this->size.height()>image.size().height()) return image;
        }
        return image.scaled(this->size, Qt::KeepAspectRatio);
}


/**
* Ładuje ponownie wszystkie obrazy
*/
void ImageLoader::reloadImages()
{
	PictureListWidgetItem *tmpItem;
	tmpItem = dynamic_cast<PictureListWidgetItem*> ( this->manager->getListWidget()->item(this->currentIndex));
        this->currentImage.load(tmpItem->getFile().filePath());
        this->currentImage = getScaledImage(this->currentImage);

        if ((this->currentIndex+1)<this->manager->getListWidget()->count()) {
                tmpItem = dynamic_cast<PictureListWidgetItem*> ( this->manager->getListWidget()->item(this->currentIndex+1));
                this->nextImage.load(tmpItem->getFile().filePath());
		this->nextImage = getScaledImage(this->nextImage);
        }

}

void ImageLoader::propertyChangedSlot(Property::PropertyType typ)
{
	if(typ == Property::ShowScalledPicture){
		this->reloadImages();
	}
}

void ImageLoader::sendNextAndReload()
{
        PictureListWidgetItem *tmpItem;
        QApplication::postEvent ( this->manager, new ImageLoadedEvent ( this->nextImage, this->currentIndex));
        this->currentImage = this->nextImage;
        if ((this->currentIndex+1)<this->manager->getListWidget()->count()) {
                tmpItem = dynamic_cast<PictureListWidgetItem*> ( this->manager->getListWidget()->item(this->currentIndex+1));
                this->nextImage.load(tmpItem->getFile().filePath());
                this->nextImage=getScaledImage(this->nextImage);
        }
}




